﻿using UnityEngine;

public static class ComponentExtension
{
    /// <summary>
    /// 尝试从子物体中获得指定组件，成功返回获得的第一个 ，失败返回默认值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="component"></param>
    /// <param name="result"></param>
    public static bool TryGetComponentInChildren<T>(this Component component, out T result)
    {
        foreach (var child in component.GetComponentsInChildren<Transform>())
        {
            if (child.GetComponent<T>() != null)
            {
                result = child.GetComponent<T>();
                return true;
            }
        }
        result = default(T);
        return false;
    }
}
